<?php
$listStr = Yii::t('site', 'Message List');
$creatorStr = Yii::t('site', 'Sender');
$timeStr = Yii::t('site', 'Time');
$nameStr = Yii::t('site', 'Name');
$actionStr = Yii::t('site', 'Action');
$addStr = Yii::t('site', 'Send Message');
$viewStr = Yii::t('site', 'View');
$prevStr = Yii::t('site', '<<');
$nextStr = Yii::t('site', '>>');
$activeStr = Yii::t('site', 'active');

$screen = Blyn::app()->getCurrentNode()->getUIManager()->getCurrentScreen();
$app = Blyn::app()->getActiveApp();

$operation = Blyn::app()->getActiveOperation();
$messages = $operation->getMessenger()->receiveMessages();

$totalRows = count($messages);
$rowsPerPage = 5;
if (!empty($_POST))
    $pageNo = $_POST['curPageNo'];
else
    $pageNo = 1;
$totalpages = ceil($totalRows / $rowsPerPage);

if ($screen->isAjaxLinkClicked($prevStr)) {
    if (!empty($_POST)) {
        $pageNo--;
        if ($pageNo < 1)
            $pageNo = 1;
    }
}

if ($screen->isAjaxLinkClicked($nextStr)) {
    if (!empty($_POST)) {
        $pageNo++;
        if ($pageNo > $totalpages)
            $pageNo = $totalpages;
    }
}

//$p2btn = $screen->buildAjaxLink('p2', '2', NULL, 'resourceList', array('update' => "#resourceDataTable", 'type' => 'post'), array('class' => 'btn'));
for ($j = 0; $j < $totalpages; $j++) {
    $page_number = strval($j + 1);
    if ($screen->isAjaxLinkClicked($page_number)) {
        if (!empty($_POST)) {
            $pageNo = $page_number;
            break;
        }
    }
}

$startNo = ($pageNo - 1) * $rowsPerPage;
if ($totalRows < $pageNo * $rowsPerPage)
    $endNo = $totalRows;
else
    $endNo = $startNo + $rowsPerPage;
?>

<div><legend><?php echo $listStr ?></legend></div>
<?php echo CHtml::beginForm(); ?>
<fieldset>
    <table class="table table-hover">
        <thead>
            <tr>
                <th><?php echo $nameStr ?></th>
                <th><?php echo $creatorStr ?></th>
                <th><?php echo $timeStr ?></th>
                <th><?php echo $actionStr ?></th>
            </tr> 
        </thead>
        <tbody>
            <?php
            $i = 0;
            foreach ($messages as $message) {
                if ($i >= $startNo && $i <= $endNo - 1) {
                    $messageId = $message->getId();
                    $senderInfo = $message->getSenderInfo();
                    $messageName = $message->getName();
                    ?>
                    <tr>
                        <td>
                            <?php echo CHtml::encode($messageName); ?>
                        </td>
                        <td>
                            <?php echo CHtml::encode($senderInfo); ?>
                        </td>
                        <td>
                            <?php echo CHtml::encode(date('Y-m-d h:i:s A', $message->sendTime)); ?>
                        </td>
                        <td>
                            <div class="row-fluid">
                                <?php
                                echo $screen->getAjaxLink(Yii::t('site', 'View'), array(
                                    'ajaxLinkId' => 'viewMessage',
                                    'ajaxContainerId' => 'messageDialog',
                                    'data' => array('currentMessageId' => $messageId),
                                    'htmlOptions' => array('class' => 'btn btn-small btn-primary'),
                                    'ajaxOptions' => array('complete' => 'function(){$("#ajax-loaderdiv").hide();$("#messageDialog").modal("show");}')));
                                ?>
                                <?php
                                $requestItem = array('messageId'=>$messageId); 
                                 echo $screen->getAjaxLink(array('name' => Yii::t('site', 'Remove'), 'id' => 'removeMessage'.$messageId), array('reqId' => $requestItem,
                                    'htmlOptions' => array('class' => 'btn btn-small btn-primary', 'enctype' => 'multipart/form-data')));
                                ?>
                               
                            </div>
                        </td>
                    </tr>
                </div>
                <?php
            }
            $i++;
        }
        ?>
        </tbody>
    </table>

    <div class="row-fluid">
        <?php
        echo $screen->getAjaxLink(Yii::t('site', 'Send Message'), array(
            'ajaxLinkId' => 'addMessage',
            'ajaxContainerId' => 'messageDialog',
            'htmlOptions' => array('class' => 'btn btn-primary'),
            'ajaxOptions' => array('complete' => 'function(){$("#ajax-loaderdiv").hide();$("#messageDialog").modal("show");}')));
        
        echo ' ';
        echo $screen->getAjaxLink(Yii::t('site', 'Test Send Operation Message'), array(
            'ajaxLinkId' => 'sendOperationMessage',
            'ajaxContainerId' => $screen->getNode()->getId(),
            'htmlOptions' => array('class' => 'btn btn-primary'),
            'ajaxOptions' => array('complete' => 'function(){$("#ajax-loaderdiv").hide();$("#refreshIt").click();}')));
        
        echo ' ';
        echo $screen->getAjaxLink(Yii::t('site', 'Test Send Role Message'), array(
            'ajaxLinkId' => 'sendRoleMessage',
            'ajaxContainerId' => $screen->getNode()->getId(),
            'htmlOptions' => array('class' => 'btn btn-primary'),
            'ajaxOptions' => array('complete' => 'function(){$("#ajax-loaderdiv").hide();$("#refreshIt").click();}')));
        ?>
    </div>
    <div id="messageDialog" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    </div>   
    <div>
        <input type="hidden" name="curPageNo" value="<?php echo $pageNo; ?>">
    </div>
    <?php echo $screen->getAjaxLink('Refresh', array('htmlOptions' => array('id' => 'refreshIt', 'style' => 'display:none'))); ?>
</fieldset>
<body>  
    <div class="pagination">  
        <ul>  
            <li <?php
            $disabled = ($pageNo == 1 || $totalpages == 0);
            if ($disabled)
                echo "class='active'";
            ?>>
                    <?php
                    if ($pageNo + 1 > $totalpages)
                        $curPageNoStr = strval($totalpages);
                    else
                        $curPageNoStr = strval($pageNo + 1);

                    if ($disabled) {
                        echo '<a href="javascript:void()">' . $prevStr . '</a>';
                    } else {
                        echo $screen->getAjaxLink($prevStr, array('data' => $curPageNoStr));
                    }
                    ?>
            </li>  
            <?php
            for ($j = 0; $j < $totalpages; $j++) {
                $curPageNoStr = strval($j + 1);
                ?>
                <li <?php
                $disabled = ($pageNo == $j + 1);
                if ($disabled)
                    echo "class='active'";
                ?>>
                        <?php
                        if ($disabled) {
                            echo '<a href="javascript:void()">' . $curPageNoStr . '</a>';
                        } else {
                            echo $screen->getAjaxLink($curPageNoStr);
                        }
                        ?>
                </li>  
            <?php }
            ?>
            <li <?php
            $disabled = ($pageNo == $totalpages || $totalpages == 0);
            if ($disabled)
                echo "class='active'";
            ?>>
                    <?php
                    if ($pageNo < 2)
                        $curPageNoStr = '1';
                    else
                        $curPageNoStr = strval($pageNo - 1);

                    if ($disabled) {
                        echo '<a href="javascript:void()">' . $nextStr . '</a>';
                    } else {
                        echo $screen->getAjaxLink($nextStr, array('data' => $curPageNoStr));
                    }
                    ?>
            </li>  
        </ul>  
    </div>  
</body>
<?php echo CHtml::endForm(); ?>
